{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "from icepyx import icesat2data as ipd\n",
    "import numpy as np\n",
    "import os\n",
    "import shutil\n",
    "import h5py\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Earthdata Login password:  ········\n"
     ]
    }
   ],
   "source": [
    "short_name = 'ATL06'\n",
    "spatial_extent = [31.5, -70.56, 33.73, -69.29]\n",
    "date_range = ['2020-03-30','2020-04-1']\n",
    "region_a = ipd.Icesat2Data(short_name, spatial_extent, date_range)\n",
    "region_a.earthdata_login('therealbradlipovsky','brad_lipovsky@fas.harvard.edu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "#region_a.order_vars.avail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_a.order_vars.append(var_list=['count'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total number of data order requests is  1  for  1  granules.\n",
      "Data request  1  of  1  is submitting to NSIDC\n",
      "order ID:  5000000701069\n",
      "Initial status of your order request at NSIDC is:  processing\n",
      "Your order status is still  processing  at NSIDC. Please continue waiting... this may take a few moments.\n",
      "Your order is: complete\n",
      "Beginning download of zipped output...\n",
      "Data request 5000000701069 of  1  order(s) is downloaded.\n",
      "Download complete\n"
     ]
    }
   ],
   "source": [
    "region_a.download_granules('/home/jovyan/data') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "FILE_NAME = 'data/processed_ATL06_20200330121520_00600712_003_01.h5'\n",
    "f = h5py.File(FILE_NAME, mode='r') \n",
    "\n",
    "count = f['gt1l/residual_histogram/count'] # has units of n_histograms, n_bins\n",
    "lat_mean = f['gt1l/residual_histogram/lat_mean']\n",
    "\n",
    "#latitude = f['/gt2r/heights/lat_ph']\n",
    "#longitude = f['/gt2r/heights/lon_ph']\n",
    "#height = f['gt2r/heights/h_ph']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.transpose(np.array(count))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "10e03985839a4300998047ae87eb1560",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "fig=plt.figure(figsize=(10,8))\n",
    "ax = fig.add_subplot(111)\n",
    "h = ax.imshow(data,vmin=0,vmax=30,cmap='Reds')\n",
    "plt.colorbar(h)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<HDF5 dataset \"lat_mean\": shape (523,), type \"<f8\">"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
